package com.hb.core.exception;

import com.hb.core.constant.Code;
import lombok.Data;

/**
 * @description: 异常处理类
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/8/13 10:03
 * @version:1.0
 */
@Data
public class ServiceException extends RuntimeException {

    private Integer errorCode;

    private String errorMessage;

    private Code code;

    public static final String DEFUAL_CODE_NAME = "comm.unkown";

    public ServiceException(Code code) {
        super(code.getMessage());
        this.code = code;
        this.errorCode = code.getCode();
        this.errorMessage = code.getMessage();
    }

    public ServiceException(Code code, String msg) {
        super(msg);
        this.code = code;
        this.errorCode = code.getCode();
        this.errorMessage = msg;
    }

    public ServiceException(int code, String msg) {
        super(msg);
        this.code = Code.newInstance(code, DEFUAL_CODE_NAME, msg);
        this.errorCode = code;
        this.errorMessage = msg;
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }

    @Override
    public String toString() {
        return String.format("{\"code\":\"%s\",\"message\":\"%s\"}", errorCode, errorMessage);
    }
}
